<?php
if(isset($_REQUEST['frm_aksi'])){
	if($_REQUEST['frm_aksi']=="cetak"){

		include_once('../class/tbs_class.php');
		include_once('../class/plugins/tbs_plugin_opentbs.php');
		require_once('../class/class.kartu.php');
		require_once('../class/class.detailkartu.php');
		require_once('../class/class.cuti.php');
		require_once('../class/class.refcuti.php');
		require_once('../class/class.batal.php');
		require_once('../class/class.pegawai.php');
		include_once('../class/class.golongan.php');
		include_once('../class/class.jabatan.php');
		include_once('../class/class.tunda.php');

		$TBS = new clsTinyButStrong;
		$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

		session_start();
		$msg=false;

		$template = '../assets/kartu.xlsx';
		$TBS->LoadTemplate($template);

		$nip=$_SESSION['nip'];

		$pegawaiDao=new PegawaiDao();
		$pegawai=$pegawaiDao->getPegawai($nip);
		$golonganDao= new GolonganDao();

		$jabatanDao=new JabatanDao();

		$golongan=$golonganDao->getGolongan($pegawai['gol']);

		$str=$jabatanDao->getJabatan($nip);

		$strJabatan=explode("\n", $str);

		$saldo=0;
		$tundaDao=new TundaDao();
		$tundanya=$tundaDao->getTundaByNip($nip,'2012');
		if($tundanya!=false){
			$saldo_awal=(int) $tundanya['jumlah'];
		}else{
			$saldo_awal=0;
		}

		$pangkat=$golongan['pangkat'];
		$gol=$golongan['nmgol'];
		$jabatan=$strJabatan[0];
		$unit=$strJabatan[1];
		$nama=$pegawai['nama'];

		$kartuDao=new KartuDao();
		$kartu=$kartuDao->getKartuByNip($nip);
		$idKartu=$kartu['id'];
		$noKartu=$kartu['nomorKartu'];
		$detailKartuDao=new DetailKartuDao();
		$details=$detailKartuDao->getAllDetail($idKartu);
		$cutiDao=new CutiDao();
		$kartunya=array();
		$cuti=array();
		$batal=array();
		$batalDao=new BatalDao();
		$refDao=new RefCutiDao();

		if($details!=false){
			for($i=0;$i<count($details);$i++){
				if($details[$i]['idBatal']!="0"){
					$cuti[$i]=$cutiDao->getCuti($details[$i]['idCuti']);
					$batal[$i]=$batalDao->getBatalById($details[$i]['idBatal']);
					$kartunya[$i]=array(
						'cuti' => $cuti[$i],
						'kartu' => $details[$i],
						'ref' => $refDao->getCuti($cuti[$i]['idJnsCuti']),
						'isBatal' => true,
						'batal' => $batal[$i],
					);
				}else{
					$cuti[$i]=$cutiDao->getCuti($details[$i]['idCuti']);
					$kartunya[$i]=array(
						'cuti' => $cuti[$i],
						'kartu' => $details[$i],
						'ref' => $refDao->getCuti($cuti[$i]['idJnsCuti']),
						'isBatal' => false,
						'batal' => false,
					);
				}
			}
		}else{
			$kartunya=false;
		}
		$data=array();
		$jmlCutinya=0;

		if($kartunya!=false){
			//$msg=true;
			for($i=0;$i<count($kartunya);$i++){
				if($kartunya[$i]['isBatal']){
					$jmlHariBatal=$kartunya[$i]['batal']['jmlHariBatal'];
					$data[$i]=array(
						'i'	=> $i+1,
						'ref' => $kartunya[$i]['ref'],
						'nomorSurat' => $kartunya[$i]['kartu']['nomorSurat'],
						'tglSurat' => $kartunya[$i]['kartu']['tglSurat'],
						'tglAwal' => $kartunya[$i]['cuti']['tglAwal'],
						'tglAkhir' => $kartunya[$i]['cuti']['tglAkhir'],
						'ket'	=> $kartunya[$i]['cuti']['jmlHari'],
						'sal' => $saldo_awal-$kartunya[$i]['cuti']['jmlHari'],			
					);
					if($kartunya[$i]['cuti']['idJnsCuti']=='11' || $kartunya[$i]['cuti']['idJnsCuti']=='12'){
						$jmlCutinya+=(int) $kartunya[$i]['cuti']['jmlHari'];
					}
					$data[$i+1]=array(
						'i'	=> $i+1,
						'ref' => $kartunya[$i]['ref'],
						'nomorSurat' => $kartunya[$i]['kartu']['nomorSurat'],
						'tglSurat' => $kartunya[$i]['kartu']['tglSurat'],
						'tglAwal' => $kartunya[$i]['cuti']['tglAwal'],
						'tglAkhir' => $kartunya[$i]['cuti']['tglAkhir'],
						'ket' => "-".$jmlHariBatal,		
						'sal' =>$saldo_awal+$jmlHariBatal,		
					);
					if($kartunya[$i]['cuti']['idJnsCuti']=='11' || $kartunya[$i]['cuti']['idJnsCuti']=='12'){
						$jmlCutinya-=(int) $jmlHariBatal;
					}
					$i++;
				}else{
					$data[$i]=array(
						'i'	=> $i+1,
						'ref' => $kartunya[$i]['ref'],
						'nomorSurat' => $kartunya[$i]['kartu']['nomorSurat'],
						'tglSurat' => $kartunya[$i]['kartu']['tglSurat'],
						'tglAwal' => $kartunya[$i]['cuti']['tglAwal'],
						'tglAkhir' => $kartunya[$i]['cuti']['tglAkhir'],
						'ket'	=> $kartunya[$i]['cuti']['jmlHari'],
						'sal' => $saldo_awal-$kartunya[$i]['cuti']['jmlHari'],			
					);
					if($kartunya[$i]['cuti']['idJnsCuti']=='11' || $kartunya[$i]['cuti']['idJnsCuti']=='12'){
						$jmlCutinya+=(int) $kartunya[$i]['cuti']['jmlHari'];
					}
				}
				
			}

		}
		$jmlData=count($data);
		$saldo=$saldo_awal-$jmlCutinya;
		$TBS->MergeBlock('a', $data);
		
		$output_file_name="kartu_cuti_".$nip."_tgl_".date('Y-m-d').".xlsx";
		$TBS->Show(OPENTBS_DOWNLOAD, $output_file_name);
	}

	if($_REQUEST['frm_aksi']=="cetakNip"){

		include_once('../class/tbs_class.php');
		include_once('../class/plugins/tbs_plugin_opentbs.php');
		require_once('../class/class.kartu.php');
		require_once('../class/class.detailkartu.php');
		require_once('../class/class.cuti.php');
		require_once('../class/class.refcuti.php');
		require_once('../class/class.batal.php');
		require_once('../class/class.pegawai.php');
		include_once('../class/class.golongan.php');
		include_once('../class/class.jabatan.php');
		include_once('../class/class.tunda.php');

		$TBS = new clsTinyButStrong;
		$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

		$idKartu=$_POST['id_aksi'];
		$kartuDao=new KartuDao();

		$datKartu=$kartuDao->getKartu($idKartu);

		$nip=$datKartu['nip'];

		$msg=false;

		$template = '../assets/kartu.xlsx';
		$TBS->LoadTemplate($template);

		

		$pegawaiDao=new PegawaiDao();
		$pegawai=$pegawaiDao->getPegawai($nip);
		$golonganDao= new GolonganDao();

		$jabatanDao=new JabatanDao();

		$golongan=$golonganDao->getGolongan($pegawai['gol']);

		$str=$jabatanDao->getJabatan($nip);

		$strJabatan=explode("\n", $str);

		$pangkat=$golongan['pangkat'];
		$gol=$golongan['nmgol'];
		$jabatan=$strJabatan[0];
		$unit=$strJabatan[1];
		$nama=$pegawai['nama'];

		$saldo=0;
		$tundaDao=new TundaDao();
		$tundanya=$tundaDao->getTundaByNip($nip,'2012');
		if($tundanya!=false){
			$saldo_awal=(int) $tundanya['jumlah'];
		}else{
			$saldo_awal=0;
		}
		
		$kartu=$kartuDao->getKartuByNip($nip);
		$idKartu=$kartu['id'];
		$noKartu=$kartu['nomorKartu'];
		$detailKartuDao=new DetailKartuDao();
		$details=$detailKartuDao->getAllDetail($idKartu);
		$cutiDao=new CutiDao();
		$kartunya=array();
		$cuti=array();
		$batal=array();
		$batalDao=new BatalDao();
		$refDao=new RefCutiDao();
		$jmlCutinya=0;
		if($details!=false){
			for($i=0;$i<count($details);$i++){
				if($details[$i]['idBatal']!="0"){
					$cuti[$i]=$cutiDao->getCuti($details[$i]['idCuti']);
					$batal[$i]=$batalDao->getBatalById($details[$i]['idBatal']);
					$kartunya[$i]=array(
						'cuti' => $cuti[$i],
						'kartu' => $details[$i],
						'ref' => $refDao->getCuti($cuti[$i]['idJnsCuti']),
						'isBatal' => true,
						'batal' => $batal[$i],
					);
					
				}else{
					$cuti[$i]=$cutiDao->getCuti($details[$i]['idCuti']);
					$kartunya[$i]=array(
						'cuti' => $cuti[$i],
						'kartu' => $details[$i],
						'ref' => $refDao->getCuti($cuti[$i]['idJnsCuti']),
						'isBatal' => false,
						'batal' => false,
					);
					
				}
			}
		}else{
			$kartunya=false;
		}
		$data=array();

		if($kartunya!=false){
			//$msg=true;
			for($i=0;$i<count($kartunya);$i++){
				if($kartunya[$i]['isBatal']){
					$jmlHariBatal=$kartunya[$i]['batal']['jmlHariBatal'];
					
					$data[$i]=array(
						'i'	=> $i+1,
						'ref' => $kartunya[$i]['ref'],
						'nomorSurat' => $kartunya[$i]['kartu']['nomorSurat'],
						'tglSurat' => $kartunya[$i]['kartu']['tglSurat'],
						'tglAwal' => $kartunya[$i]['cuti']['tglAwal'],
						'tglAkhir' => $kartunya[$i]['cuti']['tglAkhir'],
						'ket'	=> $kartunya[$i]['cuti']['jmlHari'],
						'sal' => "-".$kartunya[$i]['cuti']['jmlHari'],			
					);
					if($kartunya[$i]['cuti']['idJnsCuti']=='11' || $kartunya[$i]['cuti']['idJnsCuti']=='12'){
						$jmlCutinya+=(int) $kartunya[$i]['cuti']['jmlHari'];
					}
					$data[$i+1]=array(
						'i'	=> $i+1,
						'ref' => $kartunya[$i]['ref'],
						'nomorSurat' => $kartunya[$i]['kartu']['nomorSurat'],
						'tglSurat' => $kartunya[$i]['kartu']['tglSurat'],
						'tglAwal' => $kartunya[$i]['cuti']['tglAwal'],
						'tglAkhir' => $kartunya[$i]['cuti']['tglAkhir'],
						'ket' => "-".$jmlHariBatal,		
						'sal' =>"+".$jmlHariBatal,		
					);
					if($kartunya[$i]['cuti']['idJnsCuti']=='11' || $kartunya[$i]['cuti']['idJnsCuti']=='12'){
						$jmlCutinya-=(int) $jmlHariBatal;
					}
					$i++;
				}else{
					$data[$i]=array(
						'i'	=> $i+1,
						'ref' => $kartunya[$i]['ref'],
						'nomorSurat' => $kartunya[$i]['kartu']['nomorSurat'],
						'tglSurat' => $kartunya[$i]['kartu']['tglSurat'],
						'tglAwal' => $kartunya[$i]['cuti']['tglAwal'],
						'tglAkhir' => $kartunya[$i]['cuti']['tglAkhir'],
						'ket'	=> $kartunya[$i]['cuti']['jmlHari'],
						'sal' => "-".$kartunya[$i]['cuti']['jmlHari'],			
					);
					if($kartunya[$i]['cuti']['idJnsCuti']=='11' || $kartunya[$i]['cuti']['idJnsCuti']=='12'){
						$jmlCutinya+=(int) $kartunya[$i]['cuti']['jmlHari'];
					}
				}
				
			}

		}
		$jmlData=count($data);
		$saldo=$saldo_awal-$jmlCutinya;
		$TBS->MergeBlock('a', $data);
		
		$output_file_name="kartu_cuti_".$nip."_tgl_".date('Y-m-d').".xlsx";
		$TBS->Show(OPENTBS_DOWNLOAD, $output_file_name);
	}
}
if(isset($_REQUEST['aksi_get_kartu'])){
	require_once('../class/class.kartu.php');
	require_once('../class/class.detailkartu.php');
	require_once('../class/class.cuti.php');
	require_once('../class/class.refcuti.php');
	require_once('../class/class.batal.php');

	session_start();
	$msg=false;

	$nip=$_SESSION['nip'];
	$kartuDao=new KartuDao();
	$kartu=$kartuDao->getKartuByNip($nip);
	$idKartu=$kartu['id'];
	$noKartu=$kartu['nomorKartu'];
	$detailKartuDao=new DetailKartuDao();
	$details=$detailKartuDao->getAllDetail($idKartu);
	$cutiDao=new CutiDao();
	$kartunya=array();
	$cuti=array();
	$batal=array();
	$batalDao=new BatalDao();
	$refDao=new RefCutiDao();

	if($details!=false){
		for($i=0;$i<count($details);$i++){
			if($details[$i]['idBatal']!="0"){
				$cuti[$i]=$cutiDao->getCuti($details[$i]['idCuti']);
				$batal[$i]=$batalDao->getBatalById($details[$i]['idBatal']);
				$kartunya[$i]=array(
					'cuti' => $cuti[$i],
					'kartu' => $details[$i],
					'ref' => $refDao->getCuti($cuti[$i]['idJnsCuti']),
					'isBatal' => true,
					'batal' => $batal[$i],
				);
			}else{
				$cuti[$i]=$cutiDao->getCuti($details[$i]['idCuti']);
				$kartunya[$i]=array(
					'cuti' => $cuti[$i],
					'kartu' => $details[$i],
					'ref' => $refDao->getCuti($cuti[$i]['idJnsCuti']),
					'isBatal' => false,
					'batal' => false,
				);
			}
		}
	}else{
		$kartunya=false;
	}

	if($kartunya!=false){
		$msg=true;
	}

	$data=array(
		'msg' => $msg,
		'kartu' => $kartunya,
		);

	echo json_encode($data);
}

?>